
import csv

player_data = {}

def load_nba_csv(filename):
    try:
        nba_reader = csv.DictReader(open(filename, 'rb'))
        
    except Exception:
        print 'Error in loading file\n'

    return nba_reader

def load_data(reader):
    for player in reader:

        # Make sure this player does not exist already
        player_name = player['firstname'] + ' ' + player['lastname']

        if float(player['fga']) == 0.0:
            fg_ratio = 0
        else:
            fg_ratio = float(float(player['fgm']) / float(player['fga']))


        if float(player['fta']) == 0.0:
            ft_ratio = 0
        else:
            ft_ratio = float(float(player['ftm']) / float(player['fta']))
            
        # Create the player record entry
        player_data[player_name]= {'pts':float(player['pts']), 'off_reb': float(player['oreb']),
                                     'def_reb': float(player['dreb']),'asts': float( player['asts']),
                                   'fgm': float(player['fgm']), 'fga': float(player['fga']), 'fg-ratio': fg_ratio ,
                                   'ftm': float(player['ftm']), 'fta': float(player['fta']), 'ft-ratio': ft_ratio }

        
        
def init_data(filename):
    load_data(load_nba_csv(filename))
        
